package com.jt.www.admin.stats.service.impl;

import com.jt.www.admin.stats.service.DataStatsService;
import com.jt.www.domain.dto.*;
import com.jt.www.enums.ResultEnum;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.reps.ResultDTO;
import com.jt.www.model.vo.stats.extendbussiness.ExtendBusinessVO;
import com.jt.www.model.vo.stats.regionalunificationvo.RegionalUnificationVO;
import com.jt.www.model.vo.stats.safemanage.SafeManageConsultationSummaryVO;
import com.jt.www.model.vo.stats.safemanage.SafeManageConsultationVO;
import com.jt.www.model.vo.stats.studentbus.StudentBusSummaryVO;
import com.jt.www.model.vo.stats.studentbus.StudentBusVO;
import com.jt.www.model.vo.stats.summaryvo.SummaryToTalVO;
import com.jt.www.model.vo.stats.summaryvo.SummaryVO;
import com.jt.www.model.vo.stats.unifiedinsitemvo.ParamVO;
import com.jt.www.model.vo.stats.unifiedinsitemvo.UnifiedInsItemVO;
import com.jt.www.remote.StatisticClient;
import com.jt.www.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author admin_ltf
 * @create 2019-05-13 9:42
 */
@Service
public class DataStatsServiceImpl implements DataStatsService {

    @Autowired
    private StatisticClient statisticClient;


    @Override
    public ResponseEntity<GenericListResponse<HzbDto>> hzbStatistic(String inputDate, String branchCode) throws Exception {
        return statisticClient.hzbStatistic(inputDate, branchCode);
    }

    @Override
    public ResponseEntity<GenericListResponse<TbsfxmDto>> tbsfxmStatistic(String inputDate) throws Exception {
        return statisticClient.tbsfxmStatistic(inputDate);
    }

    @Override
    public ResponseEntity<GenericListResponse<YsywDto>> ysywStatistic(String inputDate) throws Exception {
        return statisticClient.ysywStatistic(inputDate);
    }

    @Override
    public ResponseEntity<GenericListResponse<QytbDto>> qytbStatistic(String inputDate) throws Exception {
        return statisticClient.qytbStatistic(inputDate);
    }

    @Override
    public ResponseEntity<GenericListResponse<WxbDto>> wxbStatistic(String inputDate) throws Exception {
        return statisticClient.wxbStatistic(inputDate);
    }

    @Override
    public ResponseEntity<GenericListResponse<XpxDto>> xpxStatistic(String inputDate) throws Exception {
        return statisticClient.xpxStatistic(inputDate);
    }


    @Override
    public ResultDTO searchInfoByOrgCode(String orgCode) {

        // 在这里 把所有的数据封装好 ， 有异常返回  即可。
        List<SummaryVO> list = new ArrayList<>();

        SummaryVO summaryVO = new SummaryVO();
        summaryVO.setCategory("统保示范项目");
        summaryVO.setAddComplete("1234");// 累计完成佣金
        summaryVO.setApplyMonth("12340");// 显示1-5月累计计划佣金
        summaryVO.setIndexComplete("10%");//1-5月指标完成情况
        summaryVO.setApplyMoney("123400");   // 本年计划佣金
        summaryVO.setApplyData("1%");// 本年计划完成比例
        list.add(summaryVO);
        summaryVO = new SummaryVO();
        summaryVO.setCategory("我学保");
        summaryVO.setAddComplete("1234");// 累计完成佣金
        summaryVO.setApplyMonth("12340");// 显示1-5月累计计划佣金
        summaryVO.setIndexComplete("10%");//1-5月指标完成情况
        summaryVO.setApplyMoney("123400");   // 本年计划佣金
        summaryVO.setApplyData("1%");// 本年计划完成比例
        list.add(summaryVO);


        SummaryToTalVO summaryToTalVO = new SummaryToTalVO();
        //  计算合计
        summaryToTalVO = createTotal(list);

        HashMap map = new HashMap<String, Object>();
        map.put("resultList", list);
        map.put("summaryVo", summaryToTalVO);

        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

    private SummaryToTalVO createTotal(List<SummaryVO> list) {

//        @ApiModelProperty(value = "累计完成佣金合计")
        BigDecimal addCompleteTotal = new BigDecimal("0");
//        @ApiModelProperty(ApiModelPropertyvalue = "当前已完成计划佣金.如:6月份查看报表,显示1-5月累计计划佣金")
        BigDecimal applyMonthTotal = new BigDecimal("0");
//        @ApiModelProperty(value = "当前已完成计划佣金指标已完成情况.如:6月份查看报表,1-5月指标完成情况")
        BigDecimal indexCompleteTotal = new BigDecimal("0");
//        @ApiModelProperty(value = "本年计划佣金")
        BigDecimal applyMoneyTotal = new BigDecimal("0");
//        @ApiModelProperty(value = "本年计划完成比例")
        BigDecimal applyDataTotal = new BigDecimal("0");

        for (SummaryVO vo : list) {

            String addComplete = vo.getAddComplete();
            BigDecimal bigDecimal = new BigDecimal(addComplete);
            addCompleteTotal = addCompleteTotal.add(bigDecimal);

            String applyMonth = vo.getApplyMonth();
            BigDecimal bigDecimal1 = new BigDecimal(applyMonth);
            applyMonthTotal = applyMonthTotal.add(bigDecimal1);

            String applyMoney = vo.getApplyMoney();
            applyMoneyTotal = applyMoneyTotal.add(new BigDecimal(applyMoney));

        }

        // 给 SummaryToTalVO 赋值
        SummaryToTalVO summaryToTalVO = new SummaryToTalVO();
        summaryToTalVO.setAddCompleteTotal(addCompleteTotal.toString());
        summaryToTalVO.setApplyMonthTotal(applyMonthTotal.toString());
        BigDecimal divide = addCompleteTotal.divide(applyMonthTotal, 2, BigDecimal.ROUND_DOWN);
        summaryToTalVO.setIndexCompleteTotal(divide.toString());
        summaryToTalVO.setApplyMoneyTotal(applyMoneyTotal.toString());
        BigDecimal divide1 = addCompleteTotal.divide(applyMoneyTotal, 2, BigDecimal.ROUND_DOWN);
        summaryToTalVO.setApplyDataTotal(divide1.toString());

        return summaryToTalVO;
    }


    @Override
    public ResultDTO searchUnifiedInsItem(ParamVO paramVO) {
        List<UnifiedInsItemVO> list = new ArrayList<>();
        UnifiedInsItemVO vo = new UnifiedInsItemVO();

        // 读取json文件
        String path = "src\\main\\resources\\statsjson\\UnifiedInsItem.json";
        String jsonStr = JsonUtils.parseJsonFile(path);

        HashMap map = JsonUtils.fromJson(jsonStr, HashMap.class);

//        String str = "{\"reg\":\"陕西分公司\",\"scM\":10,\"scMY\":8,\"ljPre\":80,\"day\":3,\"yearC\":83,\"yearPre\":200,\"szxPre\":20920,\"szxDay\":66,\n" +
//                "\t\"szxYear\":20920,\"xzxPre\":760,\"xzxDay\":22,\"xzxYear\":760,\"spxPre\":760,\"spxDay\":22,\"spxYear\":760,\"stuYear\":21657,\n" +
//                "\t\"stuPYear\":42000,\"insLY\":20666600.78,\"insPre\":20666600.78,\"insDay\":206.78,\"insNO\":20655.78,\"insYear\":20655.78,\n" +
//                "\t\"cmsYear\":20655.78,\"cmsLY\":20655.78,\"tbRate\":\"27.09%\",\"insL\":20655.78,\"insCDay\":20655.78,\"cmsM\":20655.78,\"cmsP\":20655.78,\n" +
//                "\t\"cmsRate\":\"27.09%\",\"cmsP1\":20655.78,\"cmsPM\":\"27.09%\",\"taskY\":20655.78,\"rateY\":\"27.09%\"}";
//        vo = JsonUtils.fromJson(str, UnifiedInsItemVO.class);
//        list.add(vo);
//        UnifiedInsItemSummaryVO svo = new UnifiedInsItemSummaryVO();
//
//        String str1 = "{\"reg\":\"合计\",\"scM\":10,\"scMY\":8,\"ljPre\":80,\"day\":3,\"yearC\":83,\"yearPre\":200,\"szxPre\":20920,\"szxDay\":66,\n" +
//                "\t\"szxYear\":20920,\"xzxPre\":760,\"xzxDay\":22,\"xzxYear\":760,\"spxPre\":760,\"spxDay\":22,\"spxYear\":760,\"stuYear\":21657,\n" +
//                "\t\"stuPYear\":42000,\"insLY\":20666600.78,\"insPre\":20666600.78,\"insDay\":206.78,\"insNO\":20655.78,\"insYear\":20655.78,\n" +
//                "\t\"cmsYear\":20655.78,\"cmsLY\":20655.78,\"tbRate\":\"27.09%\",\"insL\":20655.78,\"insCDay\":20655.78,\"cmsM\":20655.78,\n" +
//                "\t\"cmsP\":20655.78,\"cmsRate\":\"27.09%\",\"cmsP1\":20655.78,\"cmsPM\":\"27.09%\",\"taskY\":20655.78,\"rateY\":\"27.09%\"}";
//        svo = JsonUtils.fromJson(str1, UnifiedInsItemSummaryVO.class);
//
//        HashMap map = new HashMap<String,Object>();
//        map.put("resultList",list);
//        map.put("summaryVo",svo);
        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

    @Override
    public ResultDTO searchExtendedBusiness(ParamVO paramVO) {
        List<ExtendBusinessVO> list = new ArrayList<>();
        ExtendBusinessVO vo = new ExtendBusinessVO();

        // 读取json文件
        String path = "src/main/resources/statsjson/ExtendedBusiness.json";
        String jsonStr = JsonUtils.parseJsonFile(path);

        HashMap map = JsonUtils.fromJson(jsonStr, HashMap.class);
//        String str1 = "{\"reg\":\"教育行业事业部\",\"azxPre\":20655.78,\"azxDay\":20655.78,\"azxYear\":20655.78,\"azxCY\":20655.78,\n" +
//                "\t\"azxPY\":20655.78,\"azxTq\":\"35.08%\",\"xfPre\":20655.78,\"xfDay\":20655.78,\"xfYear\":20655.78,\"xfCY\":20655.78,\"colPre\":20655.78,\n" +
//                "\t\"colDay\":20655.78,\"colYear\":20655.78,\"colCY\":20655.78,\"otherPre\":20655.78,\"otherDay\":20655.78,\"otherYear\":20655.78,\n" +
//                "\t\"otherCY\":20655.78,\"insPre\":20655.78,\"insDay\":20655.78,\"comday\":20655.78,\"comP\":20655.78,\"comRate\":\"35.66%\",\n" +
//                "\t\"comYear\":20655.78,\"comLY\":20655.78,\"rateY\":\"35.66%\",\"comMP\":20655.78,\"proM\":\"35.66%\",\"taskY\":20655.78,\"rate\":\"35.66%\"}";
//
//
//       vo = JsonUtils.fromJson(str1, ExtendBusinessVO.class);
//        String str = "{\"reg\":\"合计\",\"azxPre\":20655.78,\"azxDay\":20655.78,\"azxYear\":20655.78,\"azxCY\":20655.78,\n" +
//                "\t\"azxPY\":20655.78,\"azxTq\":\"35.08%\",\"xfPre\":20655.78,\"xfDay\":20655.78,\"xfYear\":20655.78,\"xfCY\":20655.78,\"colPre\":20655.78,\n" +
//                "\t\"colDay\":20655.78,\"colYear\":20655.78,\"colCY\":20655.78,\"otherPre\":20655.78,\"otherDay\":20655.78,\"otherYear\":20655.78,\n" +
//                "\t\"otherCY\":20655.78,\"insPre\":20655.78,\"insDay\":20655.78,\"comday\":20655.78,\"comP\":20655.78,\"comRate\":\"35.66%\",\n" +
//                "\t\"comYear\":20655.78,\"comLY\":20655.78,\"rateY\":\"35.66%\",\"comMP\":20655.78,\"proM\":\"35.66%\",\"taskY\":20655.78,\"rate\":\"35.66%\"}";
//
//        ExtendBusinessSummaryVO svo = JsonUtils.fromJson(str, ExtendBusinessSummaryVO.class);
//        HashMap map = new HashMap<String,Object>();
//        map.put("resultList",list);
//        map.put("summaryVo",svo);
        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

    @Override
    public ResultDTO searchRegionalUnification(ParamVO paramvo) {
        List<RegionalUnificationVO> list = new ArrayList<>();
        RegionalUnificationVO vo = new RegionalUnificationVO();

        // 读取json文件
        String path = "src/main/resources/statsjson/RegionalUnification.json";
        String jsonStr = JsonUtils.parseJsonFile(path);

        HashMap map = JsonUtils.fromJson(jsonStr, HashMap.class);

//        String str = "{\"reg\":\"教育行业事业部\",\"itemName\":\"新疆实习支教师生校方责任保险项目\",\"insPre\":\"以往累计保费\",\"insDay\":8882345.45,\"insY\":23567,\n" +
//                "\"comY\":234789.88,\"adv\":88888\",\"inY\":3567899.88,\"comLY\":8888888,\"insMPre\":480892.989,\"insMDay\":2345567,\"comDay\":66666.99,\n" +
//                "\"comP\":200000,\"proP\":\"56%\",\"comPre\":88865,\"pro\":\"66%\",\"taskY\":999999999,\"rateY\":\"20%\"}";
//        vo = JsonUtils.fromJson(str, RegionalUnificationVO.class);
//        str = "{\"reg\":\"河南分公司\",\"itemName\":\"校（园）方责任保险项目\",\"insPre\":\"以往累计保费\",\"insDay\":8882345.45,\"insY\":23567,\n" +
//                "\"comY\":234789.88,\"adv\":88888\",\"inY\":3567899.88,\"comLY\":8888888,\"insMPre\":480892.989,\"insMDay\":2345567,\"comDay\":66666.99,\n" +
//                "\"comP\":200000,\"proP\":\"56%\",\"comPre\":88865,\"pro\":\"66%\",\"taskY\":999999999,\"rateY\":\"20%\"}";
//        vo = JsonUtils.fromJson(str, RegionalUnificationVO.class);
//
//        list.add(vo);
//        RegionalUnificationSummaryVO svo = new RegionalUnificationSummaryVO();
//        String str1 = "{\"reg\":\"合计\",\"insPre\":\"以往累计保费\",\"insDay\":8882345.45,\"insY\":23567,\n" +
//                "\"comY\":234789.88,\"adv\":88888\",\"inY\":3567899.88,\"comLY\":8888888,\"insMPre\":480892.989,\"insMDay\":2345567,\"comDay\":66666.99,\n" +
//                "\"comP\":200000,\"proP\":\"56%\",\"comPre\":88865,\"pro\":\"66%\",\"taskY\":999999999,\"rateY\":\"20%\"}";
//        svo = JsonUtils.fromJson(str1, RegionalUnificationSummaryVO.class);
//        HashMap map = new HashMap<String,Object>();
//        map.put("resultList",list);
//        map.put("summaryVo",svo);
        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

    @Override
    public ResultDTO searchILearnInsured(ParamVO paramvo) {

//        List<ILearnInsuredVO> list = new ArrayList<>();
//        ILearnInsuredVO vo = new ILearnInsuredVO();
//        list.add(vo);
//        ILearnInsuredSummaryVO svo = new ILearnInsuredSummaryVO();
//        svo.setReg("合计");
//        HashMap map = new HashMap<String,Object>();
//        map.put("resultList",list);
//        map.put("summaryVo",svo);
        // 读取json文件
        String path = "src/main/resources/statsjson/ILearnInsured.json";
        String jsonStr = JsonUtils.parseJsonFile(path);

        HashMap map = JsonUtils.fromJson(jsonStr, HashMap.class);

        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

    @Override
    public ResultDTO searchStudentSafe(ParamVO paramvo) {

//        List<StudentSafeVO> list = new ArrayList<>();
//        StudentSafeVO vo = new StudentSafeVO();
//        list.add(vo);
//        StudentSafeSummaryVO svo = new StudentSafeSummaryVO();
//        svo.setReg("合计");
//        HashMap map = new HashMap<String,Object>();
//        map.put("resultList",list);
//        map.put("summaryVo",svo);
        String path = "src/main/resources/statsjson/IStudentSafe.json";
        String jsonStr = JsonUtils.parseJsonFile(path);

        HashMap map = JsonUtils.fromJson(jsonStr, HashMap.class);
        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

    @Override
    public ResultDTO searchSchoolBus(ParamVO paramvo) {

        List<StudentBusVO> list = new ArrayList<>();
        StudentBusVO vo = new StudentBusVO();
        list.add(vo);
        StudentBusSummaryVO svo = new StudentBusSummaryVO();
        svo.setReg("合计");
        HashMap map = new HashMap<String, Object>();
        map.put("resultList", list);
        map.put("summaryVo", svo);
        return new ResultDTO(ResultEnum.SUCCESS, map);
    }

    @Override
    public ResultDTO searchSafeConsult(ParamVO paramvo) {

        List<SafeManageConsultationVO> list = new ArrayList<>();
        SafeManageConsultationVO vo = new SafeManageConsultationVO();
        list.add(vo);
        SafeManageConsultationSummaryVO svo = new SafeManageConsultationSummaryVO();
        svo.setReg("合计");
        HashMap map = new HashMap<String, Object>();
        map.put("resultList", list);
        map.put("summaryVo", svo);
        return new ResultDTO(ResultEnum.SUCCESS, map);
    }
}
